package com.baizhi.realm;

import com.baizhi.dao.AdminDAO;
import com.baizhi.entity.Admin;
import org.apache.shiro.authc.*;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.springframework.beans.factory.annotation.Autowired;

public class AuthenRealm extends AuthenticatingRealm {
    @Autowired
    private AdminDAO adminDAO;
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //获取用户输入的用户名
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
        String username = usernamePasswordToken.getUsername();
        //通过用户名，获取数据库数据
        Admin admin = adminDAO.selectByName(username);
        //将数据库查询到的数据，封装到info返回  如果数据库没有数据return null
        if(admin!=null){
            return new SimpleAuthenticationInfo(admin.getUsername(),admin.getPassword(),this.getName());
        }
        return null;

    }
}
